package com.jeecms.cms.dao.main.impl;

import com.jeecms.cms.dao.main.CmsEnterpriseDao;
import com.jeecms.cms.entity.assist.CmsEnterprise;
import com.jeecms.common.hibernate4.Finder;
import com.jeecms.common.hibernate4.HibernateBaseDao;
import com.jeecms.common.page.Pagination;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;

/**
 * Created by Hashan on 2018-02-03.
 *
 */
@Repository
public class CmsEnterpriseDaoImpl extends HibernateBaseDao<CmsEnterprise, Integer> implements CmsEnterpriseDao {
    @Override
    public CmsEnterprise findById(Integer id) {
        CmsEnterprise entity = get(id);
        return entity;
    }

    @Override
    public CmsEnterprise save(CmsEnterprise bean) {
        getSession().save(bean);
        return bean;
    }

    @Override
    public CmsEnterprise deleteById(Integer id) {
        CmsEnterprise entity = super.get(id);
        if (entity != null) {
            getSession().delete(entity);
        }
        return entity;
    }

    @Override
    public List<CmsEnterprise> getList(Map<String, Object> params) {
        Finder f = Finder.create("select bean from CmsEnterprise bean  ");
        if (params.get("branchId") != null) {
            f.append(" join bean.branch branch");
        }
        f.append(" where 1=1");
        if(params.get("branchId") != null){
            f.append(" and branch.id=:branchId");
            f.setParam("branchId", params.get("branchId"));
        }
        if (params.get("name") != null) {
            f.append(" and bean.name like :name");
            f.setParam("name", "%" + params.get("name") + "%");
        }
        if (params.get("contacts") != null) {
            f.append(" and bean.contacts=:contacts");
            f.setParam("contacts", params.get("contacts"));
        }
        if (params.get("invitationCode") != null) {
            f.append(" and bean.invitationCode=:invitationCode");
            f.setParam("invitationCode", params.get("invitationCode"));
        }
        if (params.get("legalPerson") != null) {
            f.append(" and bean.legalPerson=:legalPerson");
            f.setParam("legalPerson", params.get("legalPerson"));
        }
        if (params.get("companyId") != null) {
            f.append(" and bean.companyId<=:companyId");
            f.setParam("companyId", params.get("companyId"));
        }
        return find(f);
    }

    @Override
    public Pagination getPage(Map<String, Object> params) {
        Finder f = Finder.create("select bean from CmsEnterprise bean ");
        if (params.get("branchId") != null) {
            f.append(" join bean.branch branch");
        }
        f.append(" where 1=1");
        if(params.get("branchId") != null){
            f.append(" and branch.id=:branchId");
            f.setParam("branchId", params.get("branchId"));
        }
        if (params.get("name") != null) {
            f.append(" and bean.name like :name");
            f.setParam("name", "%" + params.get("name") + "%");
        }
        if (params.get("contacts") != null) {
            f.append(" and bean.contacts=:contacts");
            f.setParam("contacts", params.get("contacts"));
        }
        if (params.get("invitationCode") != null) {
            f.append(" and bean.invitationCode=:invitationCode");
            f.setParam("invitationCode", params.get("invitationCode"));
        }
        if (params.get("industry") != null) {
            f.append(" and bean.industry=:industry");
            f.setParam("industry", params.get("industry"));
        }
        if (params.get("legalPerson") != null) {
            f.append(" and bean.legalPerson<=:legalPerson");
            f.setParam("legalPerson", params.get("legalPerson"));
        }
        if (params.get("companyId") != null) {
            f.append(" and bean.companyId=:companyId");
            f.setParam("companyId", params.get("companyId"));
        }
        return find(f, (Integer)params.get("pageNo"), (Integer)params.get("pageSize"));
    }

    @Override
    protected Class<CmsEnterprise> getEntityClass() {
        return CmsEnterprise.class;
    }

    @Override
    public CmsEnterprise getEnterpriseByInvitationCode(String invitationCode) {
        String hql = "from CmsEnterprise bean where bean.invitationCode=?";
        return (CmsEnterprise) findUnique(hql, invitationCode);
    }

    @Override
    @SuppressWarnings("unchecked")
    public List<Object[]> findBranchTreeForEn(Integer enterpriseId) {
        String sql = "select b.name name2, b.id id2 , c.id id1, c.name name1, d.id, d.name " +
                "from jc_enterprise_branch eb, jc_branch b " +
                "left join jc_branch c on b.parent_id=c.id " +
                "LEFT JOIN jc_branch d on c.parent_id=d.id " +
                "where eb.branch_id=b.id and eb.enterprise_id="+enterpriseId+"  ORDER BY d.id ,c.id;";
        return getSession().createSQLQuery(sql).list();
    }
}
